package com.chukun.spring.configuration.metadata;

import com.chunkun.ioc.domain.User;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

import java.nio.charset.StandardCharsets;

/**
 * @author chukun
 *  根据properties 配置元信息
 */
public class PropertiesConfigurationMetaDataDemo {


    public static void main(String[] args) {
        // 创建的默认的ioc容器
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        // 创建propertiesReader对象，读取相关的外部的properties文件
        PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(beanFactory);

        // 加载properties文件
        Resource resource = new ClassPathResource("/META-INF/user-default.properties");
        // 使用 EncodedResource 解决中文乱码的相关问题
        EncodedResource encodedResource = new EncodedResource(resource, StandardCharsets.UTF_8);

        int count = reader.loadBeanDefinitions(encodedResource);

        // 依赖查找
        ObjectProvider<User> beanProvider = beanFactory.getBeanProvider(User.class);
        System.out.println(beanProvider.getIfAvailable());
    }
}
